home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / FGETPOS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  1.2 KB  |  47 lines

  1. /* fgetpos.c --- p 415 */
  2. #include <stdio.h>
  3. void read10char(FILE *, char *);
  4. main()
  5. {
  6.     fpos_t curpos;
  7.     FILE *infile;
  8.     char file3[81], buffer[40];
  9.     printf ("Enter name of a text file: ");
  10.     gets(file3);
  11.                     /* Open the file for reading */
  12.     if ((infile = fopen(file3, "r")) == NULL)
  13.     {
  14.         printf("fopen failed.\n");
  15.         exit(0);
  16.     }
  17.     read10char(infile, buffer);
  18.                     /* Save current position */
  19.     if (fgetpos(infile, &curpos) != 0)
  20.        perror("fgetpos failed!");
  21.                     /* Read another 10 characters */
  22.     read10char(infile, &buffer[11]);
  23.                     /* Reset to previous position in file */
  24.     if (fsetpos(infile, &curpos) != 0)
  25.        perror("fsetpos failed!");
  26.             /* Read another 10 characters -- these should be same
  27.                      * as last 10.  */
  28.     read10char(infile, &buffer[21]);
  29.     buffer[32] = '\0';                /* Convert to C string */
  30.     printf ("Buffer now has:\n%s", buffer);
  31. }
  32.                     /******************************/
  33. void read10char(FILE *infile, char *buffer)
  34. {
  35.     int i;
  36.     for (i=0; i<10; i++)
  37.     {
  38.          if ((*buffer = fgetc(infile)) == EOF)
  39.          {
  40.               printf("file ended. buffer so far has: \%s\n",
  41.                             buffer);
  42.               exit(0);
  43.          }
  44.          buffer++;
  45.     }
  46.     *buffer = '\n';
  47. }